﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ChuanYe.Core.Utils
{
    public class SundryHelper
    {
        #region 取得某月的第一天
        /// <summary>
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        public static DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        #endregion

        #region 取得某月的最后一天
        //// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        public static DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
        #endregion

        public static bool IsNumber(string input)
        {
            return Regex.IsMatch(input, @"^[+-]?\d*$");
        }

        public static DateTime StartTimeConvert(DateTime startTime)
        {
            return Convert.ToDateTime(Convert.ToDateTime(startTime).ToShortDateString() + " 00:00:00");
        }

        public static DateTime EndTimeConvert(DateTime endTime)
        {
            return Convert.ToDateTime(Convert.ToDateTime(endTime).ToShortDateString() + " 23:59:59");
        }
        public static DateTime LongToDateTime(long stamp)
        {
            try
            {
                DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
                if (stamp.ToString().Length == 10) StartDateTime.AddSeconds(stamp);
                return StartDateTime.AddMilliseconds(stamp);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }

        public static string GetLoumaNumber(int n)
        {
            int[] arabic = new int[13];
            string[] roman = new string[13];
            int i = 0;
            string o = "";

            arabic[0] = 1000;
            arabic[1] = 900;
            arabic[2] = 500;
            arabic[3] = 400;
            arabic[4] = 100;
            arabic[5] = 90;
            arabic[6] = 50;
            arabic[7] = 40;
            arabic[8] = 10;
            arabic[9] = 9;
            arabic[10] = 5;
            arabic[11] = 4;
            arabic[12] = 1;

            roman[0] = "M";
            roman[1] = "CM";
            roman[2] = "D";
            roman[3] = "CD";
            roman[4] = "C";
            roman[5] = "XC";
            roman[6] = "L";
            roman[7] = "XL";
            roman[8] = "X";
            roman[9] = "IX";
            roman[10] = "V";
            roman[11] = "IV";
            roman[12] = "I";

            while (n > 0)
            {
                while (n >= arabic[i])
                {
                    n = n - arabic[i];
                    o = o + roman[i];
                }
                i++;
            }
            return o;
        }
    }
}
